package com.yangcangclub.api.v1.banner.controller;

import com.alibaba.druid.util.StringUtils;
import com.yangcangclub.banner.service.BannerService;
import com.yangcangclub.commons.annotation.*;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.result.ResultData;
import com.yangcangclub.commons.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 轮播图 controller
 * Created by flyger on 2017/2/22.
 */
@Controller
@RequestMapping("/yangcangclub/v1/banner")
public class BannerController {
    private static Logger logger = LoggerFactory.getLogger(BannerController.class);

    @Autowired
    BannerService bannerService;

    /**
     * 前端轮播图展现
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/show/list")
    @ApolloSecurity(auth = AuthEnum.PUBLIC,limit = 50,time = TimeEnum.MINUTE)
    public ResultData getShowBannerList(@GetUser User user,
                                        @RequestParam(required=false,defaultValue = "1")int pageNum,
                                        @RequestParam(required=false,defaultValue = "10")int pageSize){
        ResultData resultData ;
        try {
            //校验
            //业务
            Map<String,Object> map=bannerService.getShowBannerList(pageNum,pageSize);
            resultData = new ResultData(MessageEnum.SUCCESS,map);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }
    
    /*后台管理*/

    /**
     * 添加
     * @param user
     * @param name
     * @param url
     * @param image
     * @param startDate
     * @param endDate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add")
    @ApolloSecurity(auth = AuthEnum.ADMIN,limit = 50,time = TimeEnum.MINUTE)
    public ResultData addBanner(@GetUser User user,
                                @RequestParam(required=false,defaultValue = "")String name,
                                @RequestParam(required=false,defaultValue = "")String url,
                                @RequestParam(required=false,defaultValue = "")String image,
                                @RequestParam(required=false,defaultValue = "")String startDate,
                                @RequestParam(required=false,defaultValue = "")String endDate){
        ResultData resultData ;
        try {
            //校验
            if( StringUtils.isEmpty(image)){
                throw new ApolloException(MessageEnum.PARMA_IS_EMPTY);
            }

            int createUserId = user.getId();
            //业务
            boolean flag = bannerService.saveBanner(name, url, image,startDate, endDate, createUserId);
            if(flag){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else {
                resultData = new ResultData(MessageEnum.FAILURE);
            }

        } catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /**
     * 修改
     * @param user
     * @param id
     * @param name
     * @param image
     * @param url
     * @param startDate
     * @param endDate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit")
    @ApolloSecurity(auth = AuthEnum.ADMIN,limit = 50,time = TimeEnum.MINUTE)
    public ResultData editBanner(@GetUser User user,int id, String name, String image,
                                 String url,String startDate,String endDate){
        ResultData resultData ;
        try {
            //校验
            if(id<1)throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);

            //业务
            boolean flag = bannerService.editBanner(id, name, image, url,startDate, endDate);
            if(flag){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else {
                resultData = new ResultData(MessageEnum.FAILURE);
            }

        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /**
     * 更新状态
     * @param user
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update/status")
    @ApolloSecurity(auth = AuthEnum.ADMIN,limit = 50,time = TimeEnum.MINUTE)
    public ResultData updateBannerStatus(@GetUser User user,int id,int status){
        ResultData resultData ;
        try {
            //校验 
            if(status!=1&&status!=2)throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            //业务
            boolean flag = bannerService.updateBannerPublishStatus(id,status,user.getId(),user.getName());
            if(flag){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else {
                resultData = new ResultData(MessageEnum.FAILURE);
            }
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /**
     * 管理列表
     * @param offset
     * @param limit
     * @param search
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/manager/list",method= RequestMethod.GET)
    @ApolloSecurity(auth = AuthEnum.ADMIN,limit = 20,time = TimeEnum.MINUTE)
    public ResultData getManagerList(String offset,String limit, String search) {
        ResultData  resultData ;
        try {
            // 1. 参数检测
            if(limit==null|| !StringUtil.isNumber(limit)){
                throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            }else if(offset==null|| !StringUtil.isNumber(offset)){
                throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            }

            if(StringUtil.isNotEmpty(search)){
                search=  java.net.URLDecoder.decode(search,"utf-8");
            }
            // 2. 获得列表
            Map<String, Object> map= this.bannerService.getManagerBannerList(Integer.valueOf(offset),Integer.valueOf(limit),search);
            resultData=new ResultData(MessageEnum.SUCCESS,map);
        } catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }

        return resultData;
    }

    /**
     * 移动
     */
    @ResponseBody
    @RequestMapping(value = "/move")
    @ApolloSecurity(auth = AuthEnum.ADMIN,limit = 20,time = TimeEnum.MINUTE)
    public ResultData moveBanner(String id,String type,String orderSort) {
        ResultData  resultData ;
        try {
            boolean flag =  this.bannerService.moveBanner(Integer.valueOf(id),type, Integer.valueOf(orderSort));
            resultData=new ResultData(MessageEnum.SUCCESS,flag);
        } catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }

        return resultData;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method= RequestMethod.GET)
    @ApolloSecurity(auth = AuthEnum.ADMIN,limit = 20,time = TimeEnum.MINUTE)
    public ResultData delBanner(String ids) {
        ResultData  resultData ;
        try {
            boolean flag =  this.bannerService.deleteBanner(ids);
            resultData=new ResultData(MessageEnum.SUCCESS,flag);
        } catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }




}
